package com.qf.controller;

import com.qf.service.*;
import com.qf.util.ResponseUtil;
import com.qf.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@CrossOrigin//跨域 （注解 ）
@RequestMapping("/admin/dashboard")
public class DashboardController {

    @Autowired
    private DtsGoodsService dtsGoodsService;
    @Autowired
    private DtsUserService dtsUserService;
    @Autowired
    private DtsGoodsProductService dtsGoodsProductService;
    @Autowired
    private DtsOrderService dtsOrderService;
    @Autowired
    private DtsCategoryService dtsCategoryService;

    //定个查询周期 30天
    private static final int STATIS_DAYS_RANG = 30;// 统计的天数范围，一个月数据
    @GetMapping
    public Object dashboard(){

        Integer goodsTotal = dtsGoodsService.findCount();
        Integer userTotal = dtsUserService.findCount();
        Integer productTotal = dtsGoodsProductService.findCount();
        Integer orderTotal = dtsOrderService.findCount();


        Map<String,Object> result = new HashMap<>();
        result.put("goodsTotal",goodsTotal);
        result.put("userTotal",userTotal);
        result.put("productTotal",productTotal);
        result.put("orderTotal",orderTotal);
        return ResponseUtil.ok(result);
    }
    @GetMapping("/chart")
    public Object chart(){

        List<CategorySellAmts> categorySellAmtsList =  dtsCategoryService.categorySell();
        //1:categorySell
        CategorySellVo categorySellVo = new CategorySellVo();
        categorySellVo.setCategorySellData(categorySellAmtsList);
        //一级大类目录名称
        String[] categoryNames =
                categorySellAmtsList.stream()
                        .map(CategorySellAmts::getName)
                        .collect(Collectors.toList())
                        .toArray(new String[categorySellAmtsList.size()]);
        categorySellVo.setCategoryNames(categoryNames);


/*        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE,-30);
        Date time = calendar.getTime();//过去30*/


        //2:orderAmts : 日期和对应的订单总额, 订单笔数
        List<DayStatis> orderDayStatisList = dtsOrderService
                .findOrderDayStatis(STATIS_DAYS_RANG);
        OrderAmtsVo orderAmtsVo = fetchOrderAmtsVo(orderDayStatisList);
        //3:userOrderCnt : 日期对应的用户数和订单数
        List<DayStatis> userDayStatisList = dtsUserService.findUserDayStatis(STATIS_DAYS_RANG);
        //日期的合并
        List<String> dayDataList = findDayData(orderDayStatisList,userDayStatisList);
        UserOrderCntVo userOrderCntVo = new UserOrderCntVo();
        userOrderCntVo.setDayData(dayDataList);
        List<Integer> userCnt =  fetchOrderAndUserCnt(userDayStatisList,dayDataList);
        List<Integer> orderCnt =  fetchOrderAndUserCnt(orderDayStatisList,dayDataList);
        userOrderCntVo.setUserCnt(userCnt);
        userOrderCntVo.setOrderCnt(orderCnt);
        //结果返回
        Map<String,Object> result = new HashMap<>();
        result.put("categorySell",categorySellVo);
        result.put("orderAmts",orderAmtsVo);
        result.put("userOrderCnt",userOrderCntVo);
        return ResponseUtil.ok(result);
    }

    //获取日期对应的 订单数 或用户数
    private List<Integer> fetchOrderAndUserCnt(List<DayStatis> dayStatisList, List<String> dayDataList) {
        List<Integer> list = new ArrayList<>();
        for (String dayData : dayDataList) {
            // 12-10
            boolean flag = false;
            for (DayStatis dayStatis : dayStatisList) {
                if(dayData.equals(dayStatis.getDayStr())){
                    //产生了订单数或用户数
                    list.add(dayStatis.getCnts());
                    flag = true;
                    break;
                }
            }
            //上面的If语句没进去 证明当前没有产生订单数或用户数
            if(!flag){
                list.add(0);
            }

        }
        return list;
    }

    //日期合并
    private List<String> findDayData(List<DayStatis> orderDayStatisList, List<DayStatis> userDayStatisList) {
        Set<String> set = new HashSet<>();

        for (DayStatis dayStatis : orderDayStatisList) {
            set.add(dayStatis.getDayStr());
        }
        for (DayStatis dayStatis : userDayStatisList) {
            set.add(dayStatis.getDayStr());
        }
        //排序
        //set.stream().sorted((s1,s2) -> s1.compareTo(s2));//由小到大
        //set.stream().sorted().collect(Collectors.toSet()).toArray(new String[0]);//由小到大
        return set.stream().sorted().collect(Collectors.toList());//由小到大

    }

    //日期对应订单总金额及订单笔数
    private OrderAmtsVo fetchOrderAmtsVo(List<DayStatis> orderDayStatisList) {
        OrderAmtsVo  vo = new OrderAmtsVo();
        //日期数组
        String[] dayData = new String[orderDayStatisList.size()];
        for (int i = 0; i < orderDayStatisList.size(); i++) {
            dayData[i] = orderDayStatisList.get(i).getDayStr();
        }
        vo.setDayData(dayData);
        //金额数组
        BigDecimal[] orderAmtData = orderDayStatisList.stream().map(DayStatis::getAmts).toArray((value) -> {
            System.out.println("是长度吗？：" + value);
            return new BigDecimal[value];
        });
        vo.setOrderAmtData(orderAmtData);
        //订单笔数数组
        Integer[] orderCntData = orderDayStatisList.stream().map(DayStatis::getCnts).toArray((value) -> {
            System.out.println("是长度吗？：" + value);return new Integer[value];
        });
        vo.setOrderCntData(orderCntData);
        return vo;

    }
}
